﻿using LuShanTea.DTO;
using LuShanTea.Front.Web.App_Start;
using LuShanTea.Front.Web.Models;
using LuShanTea.IService;
using LuShanTea.Web.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace LuShanTea.Front.Web.Controllers
{
    public class UserAddressController : Controller
    {
        public IUserService UserService { get; set; }

        public IAddressService AddressService { get; set; }

        public IAreaService AreaService { get; set; }

        public ICityService CityService { get; set; }

        public IProvinceService ProvinceService { get; set; }

        [IsLogin]
        public ActionResult Index()
        {

             long userid = UserHelper.GetUserId(HttpContext);

            //获取用户数据
            var user = UserService.GetById(userid);

            //判断用户是否存在
            if (user == null)
            {
                return RedirectToAction("Index", "Home");
            }

            ViewBag.User = user;

            var provinces = ProvinceService.GetAll();

            var address = AddressService.GetAddressByUserId(userid);

            UserAddressModel model = new UserAddressModel();
            model.Address = address;
            model.Provinces = provinces;

            return View(model);
        }

        [IsLogin]
        [HttpPost]
        public ActionResult Add(UserAddressAddModel model)
        {
            //验证提交数据
            if (!ModelState.IsValid)
            {
                string errors = MVCHelper.GetValidMsg(ModelState);
                return Json(new AjaxResult { Status = "error", ErrorMsg = errors });
            }

             long userid = UserHelper.GetUserId(HttpContext);

            //获取用户数据
            var user = UserService.GetById(userid);

            //判断用户是否存在
            if (user == null)
            {
                return Json(new AjaxResult
                {
                    Status = "error",
                    ErrorMsg = "没有登录"
                });
            }

            #region 判断收货地址是否超过7条
            int count = AddressService.GetAddressByUserCount(userid);

            if (count > 7)
            {
                return Json(new AjaxResult
                {
                    Status = "error",
                    ErrorMsg = "收货地址不能超过7条"
                });
            }
            #endregion

            #region 判断区域是否存在
            var area = AreaService.GetById(model.AreaId);

            if (area == null)
            {
                return Json(new AjaxResult
                {
                    Status = "error",
                    ErrorMsg = "not found area"
                });
            }

            #endregion



            AddressAddNewDTO addressAddNewDTO = new AddressAddNewDTO();
            addressAddNewDTO.AreaId = model.AreaId;
            addressAddNewDTO.PhoneNum = model.PhoneNum;
            addressAddNewDTO.PostEmail = model.PostEmail;
            addressAddNewDTO.ReciverAddress = model.ReciverAddress;
            addressAddNewDTO.ReciverName = model.ReciverName;
            addressAddNewDTO.UserId = userid;

            AddressService.AddNew(addressAddNewDTO);

            return Json(new AjaxResult { Status = "ok" });
        }

        [IsLogin]
        public ActionResult Edit(long id)
        {
             long userid = UserHelper.GetUserId(HttpContext);

            //获取用户数据
            var user = UserService.GetById(userid);

            //判断用户是否存在
            if (user == null)
            {
                return RedirectToAction("Index", "Home");
            }

            #region 判断地址是否存在
            //判断收货地址是否属于该用户
            bool isHas = AddressService.HasAddressByUser(userid, id);

            if (!isHas)
            {
                return RedirectToAction("Index", "UserAddress");
            }
            #endregion

            #region 获取要修改的地址和用户的地址
            var address = AddressService.GetByAddressId(id);

            var addresses = AddressService.GetAddressByUserId(userid);
            #endregion

            #region 获取省
            var provinces = ProvinceService.GetAll();
            #endregion

            UserAddressEditModel model = new UserAddressEditModel();
            model.AreaId = address.AreaId;
            model.CityId = address.CityId;
            model.Id = id;
            model.ProvinceId = address.ProvinceId;
            model.Provinces = provinces;
            model.Addresses = addresses;
            model.PhoneNum = address.PhoneNum;
            model.PostEmail = address.PostEmail;
            model.ReciverName = address.ReciverName;
            model.ReciverAddress = address.Adress;


            ViewBag.User = user;
            return View(model);
        }

        [IsLogin]
        [HttpPost]
        public ActionResult Edit(UserAddressAddModel model)
        {
            //验证提交数据
            if (!ModelState.IsValid)
            {
                string errors = MVCHelper.GetValidMsg(ModelState);
                return Json(new AjaxResult { Status = "error", ErrorMsg = errors });
            }

             long userid = UserHelper.GetUserId(HttpContext);

            //获取用户数据
            var user = UserService.GetById(userid);

            //判断用户是否存在
            if (user == null)
            {
                return Json(new AjaxResult
                {
                    Status = "error",
                    ErrorMsg = "没有登录"
                });
            }

            #region 判断收货地址是否超过7条
            int count = AddressService.GetAddressByUserCount(userid);

            if (count > 7)
            {
                return Json(new AjaxResult
                {
                    Status = "error",
                    ErrorMsg = "收货地址不能超过7条"
                });
            }
            #endregion

            #region 判断区域是否存在
            var area = AreaService.GetById(model.AreaId);

            if (area == null)
            {
                return Json(new AjaxResult
                {
                    Status = "error",
                    ErrorMsg = "not found area"
                });
            }

            #endregion



            AddressAddNewDTO addressAddNewDTO = new AddressAddNewDTO();
            addressAddNewDTO.Id = model.Id;
            addressAddNewDTO.AreaId = model.AreaId;
            addressAddNewDTO.PhoneNum = model.PhoneNum;
            addressAddNewDTO.PostEmail = model.PostEmail;
            addressAddNewDTO.ReciverAddress = model.ReciverAddress;
            addressAddNewDTO.ReciverName = model.ReciverName;
            addressAddNewDTO.UserId = userid;

            AddressService.Update(addressAddNewDTO);

            return Json(new AjaxResult { Status = "ok" });
        }

        [IsLogin]
        [HttpPost]
        public ActionResult Delete(long addressId)
        {
            //验证提交数据
            if (!ModelState.IsValid)
            {
                string errors = MVCHelper.GetValidMsg(ModelState);
                return Json(new AjaxResult { Status = "error", ErrorMsg = errors });
            }

             long userid = UserHelper.GetUserId(HttpContext);

            //获取用户数据
            var user = UserService.GetById(userid);

            //判断用户是否存在
            if (user == null)
            {
                return Json(new AjaxResult
                {
                    Status = "error",
                    ErrorMsg = "没有登录"
                });
            }

            //判断收货地址是否属于该用户
            bool isHas = AddressService.HasAddressByUser(userid, addressId);

            if (!isHas)
            {

                return Json(new AjaxResult
                {
                    Status = "error",
                    ErrorMsg = "not found address"
                });

            }

            AddressService.MarkDelete(addressId);

            return Json(new AjaxResult { Status = "ok" });
        }

        /// <summary>
        /// 获取省份下所有的市
        /// </summary>
        /// <param name="provinceId"></param>
        /// <returns></returns>
        [HttpPost]
        [IsLogin]
        public ActionResult GetCity(long provinceId)
        {
            var province = ProvinceService.GetById(provinceId);

            if (province == null)
            {
                return Json(new AjaxResult { Status = "error", ErrorMsg = "省份未找到" });
            }

            var cities = CityService.GetByProvId(provinceId);

            return Json(new AjaxResult { Status = "ok", Data = cities });

        }

        [IsLogin]
        [HttpPost]
        public ActionResult GetArea(long cityId)
        {
            var city = CityService.GetById(cityId);

            if (city == null)
            {
                return Json(new AjaxResult { Status = "error", ErrorMsg = "未找到改市" });
            }

            var cities = AreaService.GetByCityId(cityId);

            return Json(new AjaxResult { Status = "ok", Data = cities });

        }
    }
}